/**
 * Created by xiaolinzhang on 2016/11/1.
 */


var logger = require('../libs/log').logger;
var wxDao = require('../dao/wxDao');
var async = require('async');


exports.getProd = function (data,callback) {
    logger.debug("data"+JSON.stringify(data));
    async.series([
        function(cb){
            wxDao.getProd(data,cb);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,result);
        }
    });
}

exports.getAnn = function (data,callback) {
    async.series([
        function(cb){
            wxDao.getAnn(data,cb);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,result);
        }
    });
}

exports.getOutTradeNo = function (data,callback) {
    async.series([
        function(cb){
            wxDao.getOutTradeNo(data,cb);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,result);
        }
    });
}

exports.getUserInfo = function(data,callback){
    async.series([
        function(cb){
            wxDao.getUserInfo(data,cb);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            callback(null,result);
        }
    });
}

exports.getCardRecord = function (req,callback) {
    async.series([
        function(cb){
            var date = new Date();
            var start = (req.query.page||1)-1;
            var pageSize = req.query.pageSize||100;
            var cellno = req.body.cellno||null;
            var sdate = req.body.sdate||date.getFullYear() + "-" + (date.getMonth() < 10 ? '0' + (date.getMonth()+1) : (date.getMonth()+1)) + "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '00:00:00';
            var edate = req.body.edate||date.getFullYear() + "-" + (date.getMonth() < 10 ? '0' + (date.getMonth()+1) : (date.getMonth()+1)) + "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '23:59:59';

            var p = {
                'a.channel_id':req.body.channel_id,
                'a.emp_id':req.body.emp_id
            }
            if(cellno){
                p['a.cellno'] = cellno;
            }

            wxDao.getCardRecord(p,sdate,edate,cb);
        }
    ],function (err,result) {
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            logger.debug(JSON.stringify(result));
            callback(null,result);
        }
    });
}

exports.getEmpRank = function(data,callback){
    async.series([
        function(cb){
            wxDao.getEmpRank(data,cb);
        }
    ],function(err,result){
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            logger.debug(JSON.stringify(result));
            callback(null,result);
        }
    })
}

exports.getEmpOrdSum = function(data,callback){
    async.series([
        function(cb){
            wxDao.getEmpOrdSum(data,cb);
        }
    ],function(err,result){
        if(err){
            logger.error(err);
            callback(err,null);
        }else {
            logger.debug(JSON.stringify(result));
            callback(null,result);
        }
    })
}